<script setup lang="ts">
import { computed } from 'vue'
import { ElProgress } from 'element-plus'
import { Edit } from '@element-plus/icons-vue'

interface Props {
  id: string
  title: string
  content?: string
  status: string
  assignee?: {
    id: number
    avatar: string
    name: string
  }
  dueDate?: string
  progress?: number
  priority?: string
}

const props = defineProps<Props>()
const emit = defineEmits<{
  (e: 'edit-task', task: Props): void
}>()

// 处理拖拽开始
const handleDragStart = (e: DragEvent) => {
  e.dataTransfer?.setData('text/plain', props.id)
}

// 计算优先级样式
const priorityClass = computed(() => {
  return {
    'priority-high': props.priority === 'high',
    'priority-medium': props.priority === 'medium',
    'priority-low': props.priority === 'low'
  }
})

// 格式化日期
const formattedDate = computed(() => {
  if (!props.dueDate) return ''
  return new Date(props.dueDate).toLocaleDateString()
})

// 处理修改任务
const handleEdit = () => {
  emit('edit-task', props)
}
</script>

<template>
  <div 
    class="task-card"
    draggable="true"
    @dragstart="handleDragStart"
  >
    <div class="task-header">
      <span class="task-id">{{ id }}</span>
      <div class="task-actions">
        <div v-if="priority" class="priority-tag" :class="priorityClass">
          {{ priority }}
        </div>
        <el-button
          type="primary"
          circle
          class="edit-btn"
          @click.stop="handleEdit"
        >
          <el-icon :size="16"><Edit /></el-icon>
        </el-button>
      </div>
    </div>

    <div class="task-title">{{ title }}</div>
    
    <div v-if="content" class="task-content">{{ content }}</div>

    <div class="task-info">
      <div v-if="progress !== undefined" class="progress-section">
        <el-progress 
          :percentage="progress * 100"
          :show-text="false"
          :stroke-width="4"
        />
        <span class="progress-text">{{ Math.round(progress * 100) }}%</span>
      </div>

      <div class="task-meta">
        <div v-if="dueDate" class="due-date">
          <i class="el-icon-time"></i>
          <span>{{ formattedDate }}</span>
        </div>

        <div v-if="assignee" class="assignee">
          <el-avatar 
            :size="24"
            :src="assignee.avatar"
          >
            {{ assignee.name.charAt(0) }}
          </el-avatar>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.task-card {
  background: white;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  cursor: move;
  transition: all 0.3s;
}

.task-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.1);
}

.task-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.task-id {
  color: #909399;
  font-size: 12px;
}

.priority-tag {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  text-transform: capitalize;
}

.priority-high {
  background: #fef0f0;
  color: #f56c6c;
}

.priority-medium {
  background: #fdf6ec;
  color: #e6a23c;
}

.priority-low {
  background: #f0f9eb;
  color: #67c23a;
}

.task-title {
  color: #303133;
  font-size: 14px;
  margin-bottom: 12px;
  line-height: 1.4;
}

.task-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-text {
  color: #909399;
  font-size: 12px;
}

.task-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.due-date {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #909399;
  font-size: 12px;
}

.assignee {
  display: flex;
  align-items: center;
}

.task-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.edit-btn {
  padding: 8px;
  font-size: 16px;
}

.task-card:hover .edit-btn {
  transform: scale(1.1);
  transition: transform 0.3s ease;
}

.task-content {
  color: #606266;
  font-size: 13px;
  margin-bottom: 12px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style> 